package cn.hx.controller;

import cn.hx.entity.Project;
import cn.hx.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("searchProject")
    public String getSearchProject(Model model){
        model.addAttribute("projects",projectService.findAll());
        return "searchProject";
    }
    @GetMapping("createProject")
    public String getCreateProject(){
        return "createProject";
    }
//    修改参数回填
    @GetMapping("updateProject/{id}")
    public String getUpdateProject(@PathVariable Integer id, Model model){
        model.addAttribute("project",projectService.queryById(id));
        return "updateProject";
    }
//    修改
    @PostMapping("editProject/{id}")
    public String postEditProject(@PathVariable Integer id,Project project){
        project.setId(id);
        projectService.update(project);
        return "redirect:/searchProject";
    }
//    删除
    @GetMapping("deleteProject/{id}")
    public String getDeleteProject(@PathVariable Integer id){
        projectService.deleteById(id);
        return "redirect:/searchProject";
    }
//    新增
    @PostMapping("saveProject")
    public String postEditProject(Project project){
        projectService.insert(project);
        return "redirect:/searchProject";
    }

}
